export default {
	// 开启命名空间
	namespaced: true,

	// 数据
	state: () => ({
		address: JSON.parse(uni.getStorageSync('address') || '{}'),
		// 登录成功之后的 token 字符串
		token: uni.getStorageSync('token') || '',
		// 用户的基本信息
		userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
		// 重定向的 object 对象 { openType, from }
		redirectInfo: null,
		searchHistory: []
	}),

	// 方法
	mutations: {
		// 更新收货地址
		updateAddress(state, address) {
			state.address = address
			this.commit('m_user/saveAddressToStorage')
		},

		// 保存搜索历史记录
		searchHistoryStore(state, data) {
			if (data != null && data != undefined && data != "") {
				state.searchHistory.push(data)
				uni.setStorageSync('searchHistory', data);
			}
		},
		// 保存历史记录
		deleteSearchHistoryStore(state) {
			state.searchHistory = []
			uni.removeStorageSync('searchHistory');
		},
		// 持久化存储 address
		saveAddressToStorage(state) {
			uni.setStorageSync('address', JSON.stringify(state.address))
		},
		// 更新用户的基本信息
		updateUserInfo(state, userinfo) {
			state.userinfo = userinfo
			this.commit('m_user/saveUserInfoToStorage')
		},
		//删除用户的基本信息
		updateUserInfo(state, userinfo) {
			state.userinfo = ""
			uni.removeStorageSync("userInfo");
		},
		saveUserInfoToStorage(state) {
			uni.setStorageSync('userInfo', JSON.stringify(state.userinfo))
		},

		updateToken(state, token) {
			state.token = token
			this.commit('m_user/saveTokenToStorage')
		},

		// 将 token 字符串持久化存储到本地
		saveToken(state, token) {
			state.token = token
			uni.setStorageSync('token', token)
		},
		removeToken(state, token) {
			state.token = ""
			uni.removeStorageSync("token");
		},
		// 更新重定向的信息对象
		updateRedirectInfo(state, info) {
			state.redirectInfo = info
		},
		setUserInfo(state, data) {
			console.log('vue x user ino', data);
			state.userinfo = data
			uni.setStorageSync('userInfo', data)
		},
		//用户退出删除信息
		removeInfo(state) {
			this.commit('removeToken');
			this.commit('updateUserInfo')
		}
	},

	getters: {
		addstr(state) {
			if (!state.address.provinceName) return ''
			return state.address.provinceName + state.address.cityName + state.address.countyName + state.address
				.detailInfo
		},
		getSearchHistoryStore(state) {
			return state.searchHistory
		},
		userId(state) {
			return state.userinfo.userid;
		},
		getToken(state) {
			return state.token;
		},
		getUserInfo(state) {
			return state.userinfo;
		}
	}
}